package jmine.tec.scheduling.dao;

import static bancosys.tec.persist.hibernate.RestrictionsUtils.addRestrictionEq;
import static bancosys.tec.persist.hibernate.RestrictionsUtils.addRestrictionGe;
import static bancosys.tec.persist.hibernate.RestrictionsUtils.addRestrictionLe;

import java.util.List;

import jmine.tec.scheduling.domain.ScheduledTask;
import jmine.tec.scheduling.domain.ScheduledTaskExecution;
import jmine.tec.scheduling.domain.ScheduledTaskExecutionStatus;

import org.hibernate.Criteria;
import org.hibernate.criterion.Order;

import bancosys.tec.persist.dao.BaseDAO;
import bancosys.tec.utils.date.Timestamp;

/**
 * @author lundberg
 */
public class ScheduledTaskExecutionDAO extends BaseDAO<ScheduledTaskExecution> {

    /**
     * Finds all executions matching the given filters
     * 
     * @param task task
     * @param status task
     * @param start start
     * @param end end
     * @return list of matching executions
     */
    public List<ScheduledTaskExecution> findByTaskStatusDate(ScheduledTask task, ScheduledTaskExecutionStatus status, Timestamp start,
            Timestamp end) {
        Criteria c = this.createCriteria();
        addRestrictionEq(c, "task", task);
        addRestrictionEq(c, "status", status);
        addRestrictionGe(c, "start", start);
        addRestrictionLe(c, "end", end);
        return this.executeQuery(c);
    }

    /**
     * Finds all executions matching the given filters
     * 
     * @param task task
     * @param status task
     * @param start start
     * @param end end
     * @param order select sort order
     * @return list of matching executions
     */
    public List<ScheduledTaskExecution> findByTaskStatusDate(ScheduledTask task, ScheduledTaskExecutionStatus status, Timestamp start,
            Timestamp end, Order order) {
        Criteria c = this.createCriteria();
        addRestrictionEq(c, "task", task);
        addRestrictionEq(c, "status", status);
        addRestrictionGe(c, "start", start);
        addRestrictionLe(c, "end", end);
        if (order != null) {
            c.addOrder(order);
        }
        return this.executeQuery(c);
    }

    /**
     * Finds all executions matching the given filters
     * 
     * @param task task
     * @return list of matching executions
     */
    public List<ScheduledTaskExecution> findByTask(ScheduledTask task) {
        Criteria c = this.createCriteria();
        addRestrictionEq(c, "task", task);
        c.addOrder(Order.asc("end"));
        return this.executeQuery(c);
    }

}
